{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "  <td>\n",
    "    <a href=\"https://colab.research.google.com/github/nyandwi/machine_learning_complete/blob/main/1_data_computations_with_numpy/1_intro_to_numpy_for_data_computation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "  </td>\n",
    "  <td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*This notebook was created by [Jean de Dieu Nyandwi](https://twitter.com/jeande_d) for the love of machine learning community. For any feedback, errors or suggestion, he can be reached on email (johnjw7084 at gmail dot com), [Twitter](https://twitter.com/jeande_d), or [LinkedIn](https://linkedin.com/in/nyandwi).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Od0BVxJyFiMZ"
   },
   "source": [
    "<a name='0'></a>\n",
    "# Intro to NumPy for Data Computations\n",
    "\n",
    "This is lab is performing data computations with NumPy. NumPy is a scientific tool used to make mathematical computations easily. \n",
    "\n",
    "In this lab, you will learn to:\n",
    "\n",
    " * [1. Create a NumPy array](#1)\n",
    " * [2. Select data: indexing and slicing of array](#2)\n",
    " * [3. Perform mathematical and other basic operations](#3)\n",
    " * [4. Perform basic statistics](#4)\n",
    " * [5. Manipulate data](#5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2MG9Fii1d8Wy"
   },
   "source": [
    "If you are using Google Colab, we do not need to install NumPy. We will only have to import it just like this:\n",
    "\n",
    "`import numpy as np`\n",
    "\n",
    "If you are using local Jupyter notebooks, make sure you have it installed already."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XzRyETjqGuLE"
   },
   "source": [
    "<a name='1'></a>\n",
    "## 1. Creating an Array in NumPy\n",
    "\n",
    "Array can either be vector or matrice. A vector is one dimensional array, and a matrix is a two or more dimensional array. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "rFx8BN4bfTWL"
   },
   "outputs": [],
   "source": [
    "## Importing numpy\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "tO9qq2UEGx6M",
    "outputId": "490647a5-f8fb-4459-eeba-d3657d998afa"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Creating a simple 1 dimensional array: vector\n",
    "np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "o27WSEaWetl6",
    "outputId": "3b3406cf-31f1-483f-c0f8-0715dabf5e01"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Creating 2 dimensional array: matrix\n",
    "\n",
    "np.array([(1,2,3,4,5), (6,7,8,9,10)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "m9sI1bT0fjMK",
    "outputId": "c521c0c5-aba9-40da-aa51-ccd51e11a967"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Creating an array from a list\n",
    "\n",
    "num_list = [1,2,3,4,5]\n",
    "\n",
    "np.array(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Jo2YqycbgiHV",
    "outputId": "fb8b61f7-5a05-46d3-e688-ea82991deb2d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "print(np.array(num_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FWI8bsvmgXfs"
   },
   "source": [
    "### 1.1 Generating Array\n",
    "\n",
    "NumPy offers various options to generate an array depending on particular need, such as:\n",
    "\n",
    "* Generating identity array\n",
    "* Generating zero array of a given size\n",
    "* Generating ones array with a given size\n",
    "* Generating an array in a given range\n",
    "* Generating an array with random values\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "OTjgeBrzaDQl",
    "outputId": "2a2fd24e-5faf-4d51-c450-0249377a9842"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]\n",
      " [0. 0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "## Generating an identity array \n",
    "\n",
    "identity_array = np.identity(4)\n",
    "print(identity_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "yIt4o9lBgZqo",
    "outputId": "6632591d-0203-433c-ba07-2376d8b25790"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0.],\n",
       "       [0., 1., 0., 0.],\n",
       "       [0., 0., 1., 0.],\n",
       "       [0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Generating an identity matrix of 1s\n",
    "\n",
    "np.eye(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Bo3PEZahhnGl",
    "outputId": "903bfb68-772e-473f-9c7b-9c8461a1a7bf"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7., 0., 0., 0.],\n",
       "       [0., 7., 0., 0.],\n",
       "       [0., 0., 7., 0.],\n",
       "       [0., 0., 0., 7.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You can multiply with any constant\n",
    "\n",
    "np.eye(4) * 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4yZIO0FWhzv7",
    "outputId": "15ad3530-0084-4c15-a356-42addb755332"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Generating zero array of a given size\n",
    "# 1 dimensional zero array\n",
    "np.zeros(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Ys3edWPOigeX",
    "outputId": "4017496b-ffd9-443a-fa1c-ae167ff817ac"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Creating two dimensional array: pass the tuple of rows and columns' number\n",
    "#np.zeros((rows, columns))\n",
    "\n",
    "np.zeros((5,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "l5hEgxuYiphg",
    "outputId": "f35fae1f-9cf3-44ee-bff2-9444e4aa28ae"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Generating ones array of a given size\n",
    "# 1 dimensional one array\n",
    "\n",
    "np.ones(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "S9Tb6tjgjNIW",
    "outputId": "8216c53d-f9e6-48f7-b106-2f7a71851ac0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Creating two dimensional ones array: pass the tuple of rows and columns' number\n",
    "# np.ones((rows, columns))\n",
    "\n",
    "np.ones((5,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "iB2v6pfLjWIG",
    "outputId": "dfe3950c-8b34-48c1-8af9-27dbc48a3af3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Generating an array in a given range or interval\n",
    "\n",
    "np.arange(0,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "IldBA00wjrMc",
    "outputId": "68416c4e-5f56-4a48-df21-b62a767088b9"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## If you want to control the step size\n",
    "\n",
    "np.arange(0,20,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "kXJAdbO3j1Fv",
    "outputId": "f278ab62-d48e-4fda-b834-ca7570047ab5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  5., 10., 15., 20.])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## You can also use linspace to generate an evenly spaced numbers in a given interval\n",
    "\n",
    "np.linspace(0,20,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "m7wEgM3LkMD7",
    "outputId": "1b3fb3cd-c3d6-4596-e4c6-c91f21de6f73"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.,  25.,  50.,  75., 100.])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "np.linspace(0,100,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Gk1TCkP_kRKM",
    "outputId": "1e1075c2-47dc-4abd-a784-5d9fb88f7802"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,\n",
       "        5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "422VmvKLkUIm",
    "outputId": "3805deac-07ae-4d18-b478-e44ba12305d8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.68944519, 0.25872307, 0.7565542 , 0.68606423])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Generating an array with random values\n",
    "# Create a 1D array with 4 random numbers\n",
    "\n",
    "np.random.rand(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "_In_Ydv-kt_j",
    "outputId": "735b1c2e-da13-414e-d773-a56d20745ef7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.41979127, 0.83292096, 0.50330078, 0.17331376])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(4)\n",
    "\n",
    "#We will not get teh same values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "EYopwsICk9ZI",
    "outputId": "1c07d1f3-41bd-47cf-dac7-47f36a158666"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.88627071, 0.55624758, 0.97198928, 0.74128787, 0.02940347],\n",
       "       [0.05604389, 0.22823893, 0.52886436, 0.91998249, 0.01327729],\n",
       "       [0.74984196, 0.00163448, 0.08632411, 0.08515202, 0.70213274],\n",
       "       [0.67293052, 0.18162822, 0.38745748, 0.42938446, 0.56581595]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "6achS2SnlLg6",
    "outputId": "974f6af6-0fa5-4e1d-f0ab-3c8b85514c78"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Generate one random integer in a given range\n",
    "\n",
    "np.random.randint(5,50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "a9WGTceNlmxi",
    "outputId": "ccd0a4ca-94e2-4249-81d7-c40d4eb1e992"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 44, 15,  7, 34, 32, 38, 20, 16, 27])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Generate 10 random integers in a given range\n",
    "\n",
    "np.random.randint(5,50,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Nkz5qtZ2oUFt",
    "outputId": "f57e0bae-268a-4a8c-8dba-6d6e89ecb971"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "41"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Random see to output the same random vaues at all run time \n",
    "import random\n",
    "\n",
    "random.seed(10)\n",
    "\n",
    "random.randint(5,50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "k67GJ1K0NDU4"
   },
   "source": [
    "<a name='2'></a>\n",
    "## 2. Data Selection: Indexing and slicing an Array\n",
    "\n",
    "Indexing: Selecting individual elements from the array\n",
    "\n",
    "Slicing: Selecting group of element from the array. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "523qanvFjFD6"
   },
   "source": [
    "\n",
    "### 2.1 1D Array Indexing and Selection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "id": "HEi3p6K5NWqn"
   },
   "outputs": [],
   "source": [
    "# Creating a 1 dimensional vector\n",
    "\n",
    "array_1d = np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "osAwL_bjNqUa",
    "outputId": "00bd8764-37cd-43cd-b713-4ad2fd7d4947"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Indexing: selcting an element from an array\n",
    "\n",
    "array_1d[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "g0CV4DR4Pxrr",
    "outputId": "03054ff3-f802-41d6-ed9a-948f00d67fd5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "array_1d [-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "JROW8R8dNxPb",
    "outputId": "ad5890d9-a8a6-445b-b5bd-c4a468ddde62"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Slicing: Returning the grou of element from an array\n",
    "\n",
    "array_1d [2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XCR7_WPnUM2M"
   },
   "source": [
    "### 2.2 2D Array Indexing and Selection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "id": "0q6e67_2O60m"
   },
   "outputs": [],
   "source": [
    "## Indexing 2D array\n",
    "\n",
    "array_2d = np.array([[1,2,3],[4,5,6],[7,8,9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5v3HKOHoQP2t",
    "outputId": "628fe748-a55c-400c-bf31-5a62848574d5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Selecting individual element\n",
    "## array_2d[row][column]\n",
    "## let's select 5..that is row 1, column 1 (we start from 0!!)\n",
    "\n",
    "array_2d[1][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "cZhbAnqNR6jQ",
    "outputId": "78b52c43-6dd0-4254-9e20-51bec4bf5c3c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's select 9..that is row 2, column 2\n",
    "\n",
    "array_2d[2][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "B0GnGJCOSDka",
    "outputId": "31e447b6-3a57-453b-bfc2-bd6050f07632"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Selecting whole row\n",
    "#array_2d[row]\n",
    "\n",
    "array_2d[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "8OHO4oTHSROF",
    "outputId": "ee50a860-3233-4fb3-bfa1-a71f2f70ea2e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Selecting group of elements in 2D array\n",
    "## array_2d[rows, columns]..You select rows and columns\n",
    "\n",
    "## Let's select the first two rows\n",
    "## Rows :2 denotes that we are selecting all rows up to the second. \n",
    "## Columns : denotes that all columns are selected.\n",
    "\n",
    "\n",
    "array_2d[:2,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NIkqcMKr0xoJ",
    "outputId": "f7895d25-a1ed-4eae-d2d1-624e03541d2b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [4, 5]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "## Selecting all first two rows and first two columns\n",
    "\n",
    "array_2d[:2,0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "HPwIW3HSTPPu",
    "outputId": "a3cfa1ba-237b-4201-e6f6-7616b8b4b8da"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Above is same as\n",
    "\n",
    "array_2d[0:2,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "hLC6LNsLTdNL",
    "outputId": "61247100-b491-4a59-95a3-d2d344d83ea7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## This will return all rows, and so all columns and so same as orginal array\n",
    "array_2d[0:3,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "FNV1X16NUbpJ",
    "outputId": "7f42ed6b-2ed0-406d-dd47-1907202f0b01"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 8, 9])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## return the second row\n",
    "\n",
    "array_2d[2,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XndtDh1NUmpp",
    "outputId": "42db9c63-a4c5-47d0-8d9d-b6d7d87b61c3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6, 9])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## return the second column\n",
    "\n",
    "array_2d[:,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "N9YTUJNjUxdd",
    "outputId": "08078a90-9fe5-45f6-d618-5ba3a2ea7271"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [5, 6],\n",
       "       [8, 9]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## return the last two columns\n",
    "\n",
    "array_2d[:,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "eOcqJEbxU6iR",
    "outputId": "a0fd909a-5704-42f7-b841-663a943d3f89"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 7])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## return the first column\n",
    "\n",
    "array_2d[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "OeKEcYRCVKdJ",
    "outputId": "5b10b5a5-fdf5-47c0-bd2b-d577c51ed1ad"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## return the first row\n",
    "\n",
    "array_2d[0,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mgnarYNHVXUT"
   },
   "source": [
    "Indexing or selecting 2D array may seems confusing but when you try it multiple times, you get the idea. If you are selecting an entire row, that means the all the columns are selected (but not their all values). And vice versa. \n",
    "\n",
    "As shown below, we are selecting the first row, but as you can see all columns are selected (:).\n",
    "\n",
    "```\n",
    "array_2d[0,:]\n",
    "```\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zc1Iyb91WHxf"
   },
   "source": [
    "### 2.3 Conditional selection\n",
    "\n",
    "You can use a condition to select values in an array. Let's use comparison operators to select the values. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "id": "ANqjpFENWKCM"
   },
   "outputs": [],
   "source": [
    "## Let's create an array\n",
    "\n",
    "arr= np.array(([1,2,3],[4,5,6],[7,8,9]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Y9qhO4QaZksn",
    "outputId": "d31bfcf8-06b8-4b8d-d4da-c6478cd1d58a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Select all elements in an array which are less than 6\n",
    "\n",
    "arr[arr <6 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "GNY9S75DZusJ",
    "outputId": "0642da71-f959-4063-8984-83b55e2bd909"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 8, 9])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Select all elements in an array which are greater than 6\n",
    "\n",
    "arr[arr > 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "REbbdFOBaBc6",
    "outputId": "141149de-62ab-4427-ae0a-198117ebda1a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6, 8])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Select all even numbers in an array\n",
    "\n",
    "arr[arr % 2 ==0 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ZXeWnQ6maY8A",
    "outputId": "e8e92e29-2fe7-4c48-9bc5-0bab5cab8a57"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Select all odd numbers in an array\n",
    "\n",
    "arr[arr % 2 !=0 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "TQKfIyGzaeAD",
    "outputId": "72fa895b-2367-4c56-b3fc-76d07a834d17"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## You can also have multiple conditions\n",
    "\n",
    "## In all odd numbers, return values which are greater or equal to 5\n",
    "\n",
    "\n",
    "arr[(arr % 2 !=0 ) & (arr >=5) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "LvQZsD8UbL2I",
    "outputId": "dbe32118-6aa3-4b43-ee37-16eff39839e1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False],\n",
       "       [False, False,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Using logical selection, you can also return True for values in which a given condition is met in an array\n",
    "\n",
    "arr > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "HGCXwMc-byoh",
    "outputId": "a8003057-2167-4c15-c240-4c56dcbe3e74"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False],\n",
       "       [False, False, False],\n",
       "       [False, False, False]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## We do not have 0 in our array\n",
    "\n",
    "arr == 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "kKTlmkV1cM1F"
   },
   "source": [
    "<a name='3'></a>\n",
    "## 3. Basic Array Operations\n",
    "\n",
    "### 3.1 Quick Arithmetic operation: Addition, Subtraction, Multiplication, Division, Squaring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "id": "kVyV9yZEcphH"
   },
   "outputs": [],
   "source": [
    "# Let's create two arrays\n",
    "\n",
    "arr1 = np.arange(0,5)\n",
    "arr2 = np.arange(6,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "k-XbCI7DdGSC",
    "outputId": "b0e463c1-3906-41c7-f98d-ec53596eadc1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  8, 10, 12, 14])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Addition\n",
    "\n",
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "HRO_en3BeOEr",
    "outputId": "afba0120-901e-4c4e-8add-87aa4c326f34"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 6, 6, 6, 6])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Subtraction\n",
    "\n",
    "arr2 - arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "nQB3wfgneak3",
    "outputId": "b1e9b19f-44f5-4298-e478-ff4f07c2a231"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  7, 16, 27, 40])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Multiplication\n",
    "\n",
    "arr1 * arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "-N1pyoXjej_Y",
    "outputId": "4ca31127-9cf6-4147-d165-8f51647a3fa0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.14285714, 0.25      , 0.33333333, 0.4       ])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Division\n",
    "\n",
    "arr1 / arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "De6gp_lvetPf",
    "outputId": "aa0b12f5-ad61-4e88-e2ac-50d79ade09e1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  4,  9, 16])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Squaring\n",
    "\n",
    "arr1 ** 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Ual-ST0-fhCZ"
   },
   "source": [
    "### 3.2 Universal functions\n",
    "\n",
    "NumPy universal functions (`ufunc`) allows to compute math, trigonometric, logical and comparison operations such as sin, cos, tan, exponent(exp), log, square, greater, less, etc..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "id": "aJfRCWLcfwTr"
   },
   "outputs": [],
   "source": [
    "## creating two arrays \n",
    "\n",
    "arr1 = np.arange(0,5)\n",
    "arr2 = np.arange(6,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "a3QsqIJ4NNxf",
    "outputId": "094569fa-3458-4022-f4e2-a31aaca28f8e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  8, 10, 12, 14])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the sum of two arrays\n",
    "\n",
    "np.add(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "43f5eAagNb3J",
    "outputId": "e9c826cb-26ff-437d-cfe3-191c43fb49f9"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  7, 16, 27, 40])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the product of two arrays\n",
    "\n",
    "np.multiply(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NBG0EPBuNiDZ",
    "outputId": "3063e564-8824-41cb-f07d-af0b83c1fe0f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6, -6, -6, -6, -6])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the difference between two arrays\n",
    "\n",
    "np.subtract(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XpgZ6O50NwYa",
    "outputId": "5578dc8b-be68-4172-ca25-b34fd96afbb1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.14285714, 0.25      , 0.33333333, 0.4       ])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the division of two arrays\n",
    "\n",
    "np.divide(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "K3Bc6goeHIch",
    "outputId": "63ea938d-3a71-4066-fc7a-26f8a60ed205"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the sin of arr1\n",
    "\n",
    "np.sin(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Zj05py4IIugZ",
    "outputId": "6b4eb73e-7d44-4650-81a7-80b5c856d4b1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  0.85090352,  0.89399666, -0.80115264])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin([0,45,90,180])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "fDo3b-qRIYHR",
    "outputId": "52ad4b89-32ad-4b80-e430-fc5b337fb25e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  0.54030231, -0.41614684, -0.9899925 , -0.65364362])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the cosine of arr 1\n",
    "\n",
    "np.cos(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "FsirUyFCIit9",
    "outputId": "402832da-0a17-4a08-d79d-007ab61a8014"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  0.52532199, -0.44807362, -0.59846007])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos([0,45,90,180])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XQR8m4HxI1E8",
    "outputId": "d6434e9b-5527-4466-ac58-6455b3aa15ed"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.29100619,  0.87144798, -6.79971146, -0.45231566,  0.64836083])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the tangent(tan) of the array\n",
    "\n",
    "np.tan(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "PIOUpa_gJFuv",
    "outputId": "081b23b0-c74d-4862-b27b-d91cbbffb002"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the logarithmic(log) of the array\n",
    "\n",
    "np.log(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "b_QsfHLDJVyq",
    "outputId": "5c73fd2b-3c9a-4921-ab7c-e40b8d20b8c6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  403.42879349,  1096.63315843,  2980.95798704,  8103.08392758,\n",
       "       22026.46579481])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the exponent(exp or e^) of the array\n",
    "\n",
    "np.exp(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "s2wDCGMfJfE-",
    "outputId": "4ed21980-3170-4c47-e1b8-4d268af93560"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([      0,       1,     256,   19683, 1048576])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the power  of the array\n",
    "## Array 1 is powered array 2...0^6=0, 1^7=1, 2^8=256, etc..\n",
    "\n",
    "np.power(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "bYGyzm0lKh0b",
    "outputId": "240eb78c-74db-45e8-f2e2-a314d1e54e2d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Comparison operations return true or false\n",
    "## Arr 1 is less than arr 2...so that's false\n",
    "\n",
    "np.greater(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "rXJJKyJhLfy4",
    "outputId": "cb9c2426-1068-464c-bae3-f57bf2f5614d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Comparison operations return true or false\n",
    "## Arr 1 is less than arr 2...so that's true\n",
    "\n",
    "np.less(arr1, arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6a6Agz1yOAvn"
   },
   "source": [
    "<a name='4'></a>\n",
    "## 4. Basic Statistics\n",
    "\n",
    "With NumPy, we can compute the basic statistics such as the standard deviation (std), variance (var),mean, median, minimum value, maximum value of an array. \n",
    "\n",
    "More about NumPy statistics: https://numpy.org/doc/stable/reference/routines.statistics.html#order-statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "7ZQedUDGHW60",
    "outputId": "9828d039-3399-4ac4-ff6a-00b5a9b461d4"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Creating an array \n",
    "\n",
    "arr = np.arange(0,5)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8eXcY0qjKWtx"
   },
   "source": [
    "### 4.1 Standard Deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "t-s8L0VJQtpA",
    "outputId": "716cfeeb-d69b-47e4-cf28-f58fadf1c97b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4142135623730951"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## calculating the standard deviation of the array\n",
    "## Std is how much an element of the array deviates from the mean of the array\n",
    "\n",
    "np.std(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "PhWndlxuHnrK",
    "outputId": "608573b6-e1a4-49f6-864b-bfdbc123e578"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.118033988749895"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.array([[3,4], [5,6]])\n",
    "\n",
    "np.std(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "IH2BFcuZH_jc",
    "outputId": "530fd53e-1acd-4a70-f401-6f9455641b32"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1.])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Specifying the axis\n",
    "## By default, the std is computed on the flattened values (or converted into a single column vector)\n",
    "\n",
    "np.std(arr2, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "fSCEMvzNIdpZ",
    "outputId": "691b6163-8eed-4dfc-ae99-72fe0541fea6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5, 0.5])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(arr2, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HK8EvjAcKecA"
   },
   "source": [
    "### 4.2 Variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "3DavtYCiIjDY",
    "outputId": "9735e45c-c03b-40c6-888b-e9da82cd2b55"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the Variance (var)\n",
    "\n",
    "arr = np.arange(0,5)\n",
    "\n",
    "np.var(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Av4TeSiBI0c9",
    "outputId": "985ebbf3-f3bc-4604-fae6-e5fee28ec26c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.25"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "B_n2hLsiKq7e"
   },
   "source": [
    "### 4.3 Mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "O1rlEIMiI5aO",
    "outputId": "a20c288f-9003-43b3-e240-ad33cda96223"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the mean of the array\n",
    "\n",
    "np.mean(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "evY84_vLJqe_",
    "outputId": "1d3571d4-3f8f-477e-bb12-61e2282995d3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## mean gives the same results as the average\n",
    "np.average(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "xmHcYNRWK0tX"
   },
   "source": [
    "### 4.4 Median"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "O3ZIMTETJIyk",
    "outputId": "96fd4a52-04c4-495d-ecf7-f173bc160e1a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the median of the array\n",
    "\n",
    "np.median(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_AkHlz-RK5jP"
   },
   "source": [
    "### 4.3 Minimum and Maximum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XIoaY7s_JoQB",
    "outputId": "acc350e5-7876-40b2-b5f9-24da2add27ac"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the minimum value\n",
    "\n",
    "np.min(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "mgjxq1w1KAiH",
    "outputId": "ed372591-6272-43f6-a03b-543785ce58ca"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Calculating the maximum value\n",
    "\n",
    "np.max(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pZ3JuNw7MwaS"
   },
   "source": [
    "<a name='5'></a>\n",
    "## 5. Data Manipulation\n",
    "\n",
    "Data Manipulation is important step in Machine Learning project. Let's some of NumPy methods and functions which are useful in data manipulation. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0TalsnPCkq1X"
   },
   "source": [
    "### 5.1 Shape of the array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "id": "YG_YOgh5kwdL"
   },
   "outputs": [],
   "source": [
    "## Creating an array \n",
    "\n",
    "arr1 = np.arange(0,10)\n",
    "arr2 = np.array(([1,2,3],[4,5,6],[7,8,9]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "cubkv1UCnEbW",
    "outputId": "f8733420-89e4-4147-f7f9-6525290d9851"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "7Q3PRJN3nIZ3",
    "outputId": "f292d52b-2e0e-4205-d27f-d95570a5e852"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "YBLLedf7kzJI",
    "outputId": "3a10664b-70ac-423c-f005-8929979ae309"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "0LoJBrQSk9vk",
    "outputId": "b15f5438-702b-4cc7-bd9a-0b4665516e31"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "C5vsZtE1lSM2",
    "outputId": "4cebb28f-dbfa-43c1-83ec-ef07a2580a5e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lbjzG0Xrlcbl"
   },
   "source": [
    "### 5.2 Shaping the Array\n",
    "\n",
    "`np.reshape(array_name, newshape=(rows, columns)` or `array_name.reshape(rows, columns)` change the shape of the array. The rows and columns of the new shape has to comform with the existing data of the array. Otherwise, it won't work. Take an example, you can convert (3,3) array into (1,9) but you can't convert it into (5,5). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "GkU2npH-le_9",
    "outputId": "a4ac26c0-db89-4b11-e1c9-46dc5a71785f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7],\n",
       "       [8, 9]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### arr1 is (10,)....10 rows, 1 column. Let's reshape it into (5,2)\n",
    "np.reshape(arr1, newshape=(5,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "52ls1fjDl9m3",
    "outputId": "4bdca11e-b2af-4924-de31-24f29863f375"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7],\n",
       "       [8, 9]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## This would also work\n",
    "arr1.reshape(5,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Mh8_zejvmnRs",
    "outputId": "f720d0f5-1a55-41ed-c9ca-8d094f8405a2"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2_reshaped = arr2.reshape(9,1)\n",
    "arr2_reshaped.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "gD1_rWyumsxX",
    "outputId": "97d95a9f-c806-46e5-86c2-dd660d7465f3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2_reshaped.reshape(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Iyfh1tWr2VTe",
    "outputId": "fcbac3c5-86b0-41fd-c37a-eb350e22ce6b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## np.resize can also be used to change the shape of the array into a specific size\n",
    "\n",
    "np.resize(arr2, (1,9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jPF2gp6PoZK-"
   },
   "source": [
    "### 5.3 Copying array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "MGacJLaVoZq7",
    "outputId": "298de278-da88-4f7c-f891-64e37e3b81ea"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.arange(0,10)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ahDMt-anohhh",
    "outputId": "c65d2739-0914-41b9-9b0c-15e3ae86f2d3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1_copy = arr1.copy()\n",
    "arr1_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "id": "WQy97R5Wo375"
   },
   "outputs": [],
   "source": [
    "## Copying the values of one array into the other \n",
    "\n",
    "## Let's copy array 2 into 1 --they have the same shape\n",
    "\n",
    "arr1 = np.arange(0,6)\n",
    "arr2 = np.arange(6,12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "id": "DdE9pVVdppQ8"
   },
   "outputs": [],
   "source": [
    "## arr1 is destination, arr2 is source\n",
    "np.copyto(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "GhzDAFp2qFgu",
    "outputId": "801466ef-27ec-4e4d-9678-7b5793356227"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EScZw0b9rDV-"
   },
   "source": [
    "### 5.4 Joining arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "id": "yVm7cXphrLQD"
   },
   "outputs": [],
   "source": [
    "### Creating two arrays\n",
    "\n",
    "arr1 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr2 = np.array([[10,11,12]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "h5TXwNZErU6g",
    "outputId": "49113d3c-87dc-4643-8f43-d0be74c5460d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4,  5,  6],\n",
       "       [ 7,  8,  9],\n",
       "       [10, 11, 12]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Joining them\n",
    "\n",
    "np.concatenate((arr1, arr2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "f36TbVcsrVRg",
    "outputId": "4e785c77-4203-4bee-fe05-cddd8654147c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3, 10],\n",
       "       [ 4,  5,  6, 11],\n",
       "       [ 7,  8,  9, 12]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Transposing arr2\n",
    "## arr2.T is transpose operation\n",
    "\n",
    "np.concatenate((arr1, arr2.T), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4XrLudetsvl0",
    "outputId": "93f316b1-0fa9-4696-9455-395ddc16662b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Setting axis to none flatten the array\n",
    "\n",
    "np.concatenate((arr1, arr2), axis=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "bVQMnJais7_6",
    "outputId": "ab2f34c0-6e2a-49b9-b6fe-1ff22d264289"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  6],\n",
       "       [ 1,  7],\n",
       "       [ 2,  8],\n",
       "       [ 3,  9],\n",
       "       [ 4, 10],\n",
       "       [ 5, 11]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Joining two 1Ds array into 2D array: Stacking\n",
    "\n",
    "# Column stacking\n",
    "\n",
    "arr1 = np.arange(0,6)\n",
    "arr2 = np.arange(6,12)\n",
    "\n",
    "np.column_stack((arr1, arr2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "utJCDp08toet",
    "outputId": "aad307f9-3caf-448d-cd28-4cc062ef8cc5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Row stacking \n",
    "\n",
    "np.row_stack((arr1, arr2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pSk67COOuFJw"
   },
   "source": [
    "### 5.5 Splitting arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "uPQ3pHuyuJrb",
    "outputId": "1056b6ad-49ee-4234-cc10-0a7cc2a640b2"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.arange(0,6)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "axVESXYzufxz",
    "outputId": "a3ae64d1-2374-44e2-d99e-ba6113a36b5e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0, 1, 2]), array([3, 4, 5])]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Splitting the array into two arrays\n",
    "\n",
    "np.split(arr1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NYw7-G0Ovs9u",
    "outputId": "a15d5f79-a209-401b-9a7d-74e6216acbde"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0, 1]), array([2, 3]), array([4, 5])]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Splitting the array into three arrays\n",
    "\n",
    "np.split(arr1, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rwOOipKsxEBl"
   },
   "source": [
    "### 5.6 Adding and repeating elements in an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "tnDE_kxxxPZI",
    "outputId": "71daa4f8-fef7-46ba-ea2c-3d6c0d32af58"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.arange(0,6)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qiWApvvXxSyq",
    "outputId": "e2f29e19-340a-4d2c-b165-1d30d4c5831b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 7])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Adding the values at the end of the array\n",
    "np.append(arr1,7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "mzA7NlQKzPN4",
    "outputId": "e9216924-9372-4b9e-c0e0-3fd174673214"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 1, 2, 3, 1, 2, 3]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### Given an array, can you add itself multiple times? or repeat it?\n",
    "\n",
    "arr = np.array([[1,2,3]])\n",
    "np.tile(arr, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XCkeeaBl0lT4",
    "outputId": "202b85dd-3f21-408b-ce03-8c9767f116e3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 2, 2, 2, 3, 3, 3])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(arr,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3mg3ZMav4HIb"
   },
   "source": [
    "### 5.7 Sorting elements in an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "02qT1kUs4L01",
    "outputId": "5929d293-4a14-4ff8-f543-27ef512c2da7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 7, 7, 9]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[1,2,3,4,5,3,2,1,3,5,6,7,7,5,9,5]])\n",
    "\n",
    "np.sort(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "z_b7H94nx61s",
    "outputId": "dcadd4d3-695c-4a71-c305-f93dda910bb3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 9])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Finding the unique elements in an array\n",
    "\n",
    "arr = np.array([[1,2,3,4,5,3,2,1,3,5,6,7,7,5,9,5]])\n",
    "\n",
    "np.unique(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "assBn6MPAOaW"
   },
   "source": [
    "### 5.8 Reversing an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "E1aycT_l1Acu",
    "outputId": "e9145b3f-7f68-4d95-8d81-34f959c17fbb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## You can also flip the array\n",
    "\n",
    "arr = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5DwhyTxY17Wx",
    "outputId": "d255183a-eb16-4117-ea35-31439a19840b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 8, 9],\n",
       "       [4, 5, 6],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## Up/down flipping\n",
    "\n",
    "np.flipud(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XQTBiI5m19gh",
    "outputId": "215de3e1-9884-44be-c7b2-93e09803ee15"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 1],\n",
       "       [6, 5, 4],\n",
       "       [9, 8, 7]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## left/right flipping\n",
    "\n",
    "np.fliplr(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mBh5uGjL4ZBq"
   },
   "source": [
    "\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "---\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dIRTQbdH2wzC"
   },
   "source": [
    "That's it for NumPy. In this lab, you learned how to create an array, perform basic operations, and also how to manipulate an array. \n",
    "\n",
    "\n",
    "In the next lab, we will learn about the Pandas, another important tool used for real world data manipulation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [BACK TO TOP](#0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "name": "1. Intro to NumPy for Data Computation.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
